<template>
  <el-row>
    <el-col :span="14" class="left">
      <!-- <img src="@/assets/images/logo1.jpg" alt="" /> -->
    </el-col>
    <el-col style="margin: 13% auto" :span="5" :offset="1">
      <el-form
        :model="loginForm"
        ref="loginFormRef"
        :rules="loginFormRules"
        v-if="loginForm.isLogin"
        :class="
          loginForm.isLogin
            ? 'animate__animated animate__pulse animate__duration-0.5s'
            : 'animate__animated animate__pulse animate__duration-0.5s'
        "
      >
        <h1>登录</h1>
        <svg-icon
          style="font-size: 10px"
          class-name="add-icon"
          icon-class="eye"
        />
        <!-- <SvgIcon class-name="search-icon" icon-class="add" /> -->
        <el-form-item prop="username">
          <el-input
            style="height: 45px"
            v-model="loginForm.username"
            placeholder="账号"
            :prefix-icon="User"
          ></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            style="height: 45px"
            v-model="loginForm.password"
            placeholder="密码"
            type="password"
            show-password
            :prefix-icon="Lock"
          >
          </el-input>
        </el-form-item>
        <el-form-item class="forget">
          <el-checkbox-group v-model="loginForm.remember">
            <el-checkbox label="记住我" name="type" />
          </el-checkbox-group>
          <a>忘记密码？</a>
        </el-form-item>
        <el-form-item>
          <el-button
            style="width: 100%; height: 40px"
            type="primary"
            @click="loginSubmit"
            :loading="loading"
            >登录</el-button
          >
        </el-form-item>
        <el-form-item class="register">
          <span @click="register">注册 →</span>
        </el-form-item>
      </el-form>
      <el-form
        v-else
        :model="registerForm"
        :rules="registerFormRules"
        ref="registerFormRef"
        :class="
          loginForm.isLogin
            ? 'animate__animated animate__pulse animate__duration-0.5s'
            : 'animate__animated animate__pulse animate__duration-0.5s'
        "
      >
        <h2>注册</h2>
        <el-form-item prop="username">
          <el-input
            style="height: 45px"
            v-model="registerForm.username"
            placeholder="账号"
            :prefix-icon="User"
          ></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            style="height: 45px"
            v-model="registerForm.password"
            placeholder="密码"
            type="password"
            show-password
            :prefix-icon="Lock"
          ></el-input>
        </el-form-item>
        <el-form-item prop="repassword">
          <el-input
            style="height: 45px"
            v-model="registerForm.repassword"
            placeholder="确认密码"
            type="password"
            show-password
            :prefix-icon="Lock"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button
            style="width: 100%; height: 40px"
            type="primary"
            @click="getCode"
            >提交</el-button
          >
        </el-form-item>
        <el-form-item class="register">
          <span @click="register">← 返回</span>
        </el-form-item>
      </el-form>
    </el-col>
  </el-row>
</template>
  <script setup>
import { reactive, ref } from "vue";
import { User, Lock, View, Hide } from "@element-plus/icons-vue";
import ElementPlus from "element-plus";
import { useUserStore } from "@/stores/user.js";
import { useRouter } from "vue-router";
import { loginApi, menuApi } from "@/api/user.js";

const loading = ref(false);
// 登录数据
const loginForm = reactive({
  username: "admin",
  password: "123456",
  remember: [],
  isLogin: true,
});
const loginFormRef = ref(null);
const loginFormRules = {
  username: [
    { required: true, message: "请输入用户名", trigger: "blur" },
    { min: 3, max: 10, message: "长度在 3 到 10 个字符", trigger: "blur" },
  ],
  password: [
    { required: true, message: "请输入密码", trigger: "blur" },
    {
      pattern: /^\S{6,15}/,
      message: "请输入6-15位的非空字符",
      trigger: "blur",
    },
  ],
};
const registerFormRules = {
  username: [
    { required: true, message: "请输入用户名", trigger: "blur" },
    { min: 5, max: 10, message: "长度在 5 到 10 个字符", trigger: "blur" },
  ],
  password: [
    { required: true, message: "请输入密码", trigger: "blur" },
    {
      pattern: /^\S{6,15}/,
      message: "请输入6-15位的非空字符",
      trigger: "blur",
    },
  ],
  repassword: [
    { required: true, message: "请输入密码", trigger: "blur" },
    {
      pattern: /^\S{6,15}/,
      message: "请输入6-15位的非空字符",
      trigger: "blur",
    },
    {
      validator: (rule, value, cb) => {
        if (value !== registerForm.password) {
          cb(new Error("两次密码不一致"));
        } else {
          cb();
        }
      },
      trigger: "blur",
    },
  ],
};
// 注册数据
const registerForm = reactive({
  username: "",
  password: "",
  repassword: "",
  isLogin: true,
});
const registerFormRef = ref(null);
function register() {
  loginForm.isLogin = !loginForm.isLogin;
}
const userStore = useUserStore();
// console.log(userStore.token);
const router = useRouter();
import { addDynamicRoutes } from "@/router/permission";
const loginSubmit = () => {
  loginFormRef.value.validate(async (valid, fields) => {
    if (valid) {
      console.log("验证通过");

      const res = await loginApi(loginForm.username);
      console.log(res, "登陆成功");
      userStore.setToken(res.data.token);
      userStore.setUser(loginForm.username);
      const ress = await menuApi(loginForm.username);
      console.log(ress.data, "获取后端返回的路由");
      sessionStorage.setItem("routes", JSON.stringify(ress.data));
      // 初始化权限路由
      // initPermission(ress.data);
      addDynamicRoutes(ress.data);
      // localStorage.setItem("routes", JSON.stringify(ress.data));
      userStore.setMenu(ress.data);
      // loginFormRef.value.resetFields();
      ElMessage.success("登录成功");

      router.replace("/board");
      // loading.value = true;

      // loading.value = false;
      // if (res.code == 0) {
      //   ElMessage.success('登录成功');
      //   userStore.setToken(res.token);
      //   loginFormRef.value.resetFields();
      //   router.replace('/');
      // } else {
      //   ElMessage.error(res.message);
      // }
    } else {
      loading.value = false;
    }
  });
};
const registerSubmit = async () => {
  registerFormRef.value.validate(async (valid, fields) => {
    if (valid) {
      // const res = await registerApi(registerForm);
      // if (res.code == 0) {
      //   ElMessage.success('注册成功');
      //   registerFormRef.value.resetFields();
      //   loginForm.isLogin = !loginForm.isLogin;
      // } else {
      //   ElMessage.error(res.message);
      // }
    } else {
      console.log("error submit!", fields);
    }
  });
};
</script>
  <style lang="less" scoped>
:deep(.el-row) {
  overflow-y: auto;
  &::-webkit-scrollbar {
    width: 6px;
    border: 1px solid transparent;
  }
  &::-webkit-scrollbar-button {
    display: none;
  }
  &::-webkit-scrollbar-thumb {
    background: #dddddd;
    border-radius: 3px;
  }
}
:deep(.el-form-item__content) {
  display: flex;
  align-items: center;
  justify-content: space-between !important;
}
.left {
  height: 100vh;
  width: 100vw;
  border-radius: 0 20px 20px 0;
  background: url("../assets/images/login-bgc.jpg") center center/cover
    no-repeat;
  box-sizing: border-box;
}
.forget {
  display: flex;
  align-items: center;
  justify-content: space-between;
  color: #409eff;
}
.register {
  cursor: pointer;
  color: #409eff;
}
</style>
  